<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>js数组方法总结(红宝书) | jiaojiao&#39;s blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/jiaojiao-blog/狗狗.png">
    <meta name="description" content="jiaojiao">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/jiaojiao-blog/assets/css/0.styles.eb0c6272.css" as="style"><link rel="preload" href="/jiaojiao-blog/assets/js/app.9b9a9edd.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/3.13686b80.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/1.7e378398.js" as="script"><link rel="preload" href="/jiaojiao-blog/assets/js/23.a86a53ea.js" as="script"><link rel="prefetch" href="/jiaojiao-blog/assets/js/10.3a421832.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/11.c69afd6f.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/12.5a57ed37.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/13.9bec8c42.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/14.506f6e6c.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/15.d294a059.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/16.6c9ad749.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/17.fed338db.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/18.7c6cff98.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/19.1b119cf7.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/20.b4e422a5.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/21.d3188793.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/22.f0f65d80.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/24.b86aa33d.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/4.cd307e36.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/5.336e8cbd.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/6.8f2d8ec2.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/7.952972ee.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/8.7270e456.js"><link rel="prefetch" href="/jiaojiao-blog/assets/js/9.6322dd00.js">
    <link rel="stylesheet" href="/jiaojiao-blog/assets/css/0.styles.eb0c6272.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-5bb33761><div data-v-5bb33761><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-5bb33761 data-v-5bb33761><h3 class="title" data-v-59e6cb88>jiaojiao's blog</h3> <p class="description" data-v-59e6cb88>jiaojiao</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>jiaojiao</span>
          
        <!---->
        2022
      </a></span></div></div> <div class="hide" data-v-5bb33761><header class="navbar" data-v-5bb33761><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/jiaojiao-blog/" class="home-link router-link-active"><img src="/jiaojiao-blog/香蕉.png" alt="jiaojiao's blog" class="logo"> <span class="site-name">jiaojiao's blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/jiaojiao-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/cs_study/" class="nav-link"><i class="undefined"></i>
  cs_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/js_study/" class="nav-link"><i class="undefined"></i>
  js_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/ts_study/" class="nav-link"><i class="undefined"></i>
  ts_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/vue_study/" class="nav-link"><i class="undefined"></i>
  vue_study
</a></li></ul></div></div><div class="nav-item"><a href="/jiaojiao-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/jiaojiao-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/读书笔记/" class="nav-link"><i class="undefined"></i>
  读书笔记
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/life/" class="nav-link"><i class="undefined"></i>
  生活
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/webDiary/" class="nav-link"><i class="undefined"></i>
  前端日记
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/li-jiao-is-scorched-green" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-5bb33761></div> <aside class="sidebar" data-v-5bb33761><div class="personal-info-wrapper" data-v-1fad0c41 data-v-5bb33761><img src="/jiaojiao-blog/头像.jpg" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    jiaojiao
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>14</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>5</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/jiaojiao-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/cs_study/" class="nav-link"><i class="undefined"></i>
  cs_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/js_study/" class="nav-link"><i class="undefined"></i>
  js_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/ts_study/" class="nav-link"><i class="undefined"></i>
  ts_study
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/categories/vue_study/" class="nav-link"><i class="undefined"></i>
  vue_study
</a></li></ul></div></div><div class="nav-item"><a href="/jiaojiao-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/jiaojiao-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      笔记
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/读书笔记/" class="nav-link"><i class="undefined"></i>
  读书笔记
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/life/" class="nav-link"><i class="undefined"></i>
  生活
</a></li><li class="dropdown-item"><!----> <a href="/jiaojiao-blog/docs/webDiary/" class="nav-link"><i class="undefined"></i>
  前端日记
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/li-jiao-is-scorched-green" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><a href="/jiaojiao-blog/docs/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/" aria-current="page" class="sidebar-link">读书笔记</a></li><li><a href="/jiaojiao-blog/docs/读书笔记/js数组方法总结.html" class="active sidebar-link">js数组方法总结(红宝书)</a></li><li><a href="/jiaojiao-blog/docs/读书笔记/ES6迭代器.html" class="sidebar-link">ES6迭代器(犀牛书)</a></li><li><a href="/jiaojiao-blog/docs/读书笔记/ES6生成器.html" class="sidebar-link">ES6生成器(犀牛书)</a></li><li><a href="/jiaojiao-blog/docs/读书笔记/元编程.html" class="sidebar-link">元编程(犀牛书)</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-5bb33761><h3 class="title" data-v-59e6cb88>js数组方法总结(红宝书)</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>jiaojiao</span>
          
        <!---->
        2022
      </a></span></div></div> <div data-v-5bb33761><div data-v-5bb33761><main class="page" style="padding-right:0;"><section style="display:;"><div class="page-title"><h1 class="title">js数组方法总结(红宝书)</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>jiaojiao</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>7/19/2022</span></i> <i class="iconfont reco-eye" data-v-8a445198><span id="/jiaojiao-blog/docs/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/js%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E6%80%BB%E7%BB%93.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-8a445198><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>js</span></i></div></div> <div class="theme-reco-content content__default"><h4 id="数组实例方法-加粗即改变原数组"><a href="#数组实例方法-加粗即改变原数组" class="header-anchor">#</a> 数组实例方法(加粗即改变原数组)</h4> <p><strong>splice</strong>(),slice(),join(),every(),some(),forEach(),map(),filter(),reduce(),reduceRight(),
<strong>sort</strong>(),<strong>reverse</strong>(),concat(),<strong>pop</strong>(),<strong>push</strong>(),<strong>shift</strong>(),<strong>unshift</strong>(),<strong>fill</strong>(),<strong>copyWithin</strong>(),
indexof(),lastindexof(),includes(),toLocalestring(),toString(),valueOf(),find(),findIndex(),keys(),values(),entries()</p> <h5 id="_1-迭代器方法-3"><a href="#_1-迭代器方法-3" class="header-anchor">#</a> 1. 迭代器方法(3)</h5> <ol><li><p>keys()</p> <p>返回数组索引的迭代器</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
  <span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;f&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;g&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;d&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">let</span> item <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p>values()</p> <p>返回数组元素的迭代器</p> <p>==同上==</p></li> <li><p>entries()</p> <p>返回键值对的迭代器</p> <p>==同上==</p></li></ol> <h5 id="_2-复制和填充方法-2"><a href="#_2-复制和填充方法-2" class="header-anchor">#</a> 2.复制和填充方法(2)</h5> <ol><li><p>fill(item,begin,end)</p> <p>fill方法用于将范围[begin,end)填充为item</p> <ol><li>如果begin或者end为负数，则通过加上length将其转为正数</li> <li>如果没有begin和end参数，则将数组所有元素变为item</li> <li>如果没有end参数，则将从begin开始的所有元素变为item</li> <li>fill()自动忽略超出数组边界，0长度，以及方向相反的索引范围</li></ol></li> <li><p>copyWithin(start,begin,end)</p> <p>copyWithin方法用于将范围[begin,end)的元素从索引start开始复制</p> <ol><li>fill()自动忽略超出数组边界，0长度，以及方向相反的索引范围</li> <li>如果没有begin,end参数,则从start开始复制整个数组，直到数组末尾</li> <li>如果没有end参数，则将begin开始的所有元素从start开始复制</li> <li>如果start或begin或者end为负数，则通过加上length将其转为正数</li></ol></li></ol> <h5 id="_3-转换方法-4"><a href="#_3-转换方法-4" class="header-anchor">#</a> 3.转换方法(4)</h5> <ol><li><p>toLocaleString()</p> <p>返回用逗号分隔的数组元素的字符串，同arr.join(','),与toString()的不同点在于会对数组的子元素调用toLocaleString()方法</p></li> <li><p>toString()</p> <p>返回用逗号分隔的数组元素的字符串，同arr.join(','),与toLocaleString()的不同点在于会对数组的子元素调用toString()方法</p> <p>(直接用alert显示数组会隐式调用数组的toString方法)</p></li> <li><p>join()</p> <p>参数为字符串，返回用参数字符串拼接所有数组元素的字符串</p></li> <li><p>valueOf()</p> <p>同toString()</p></li></ol> <h5 id="_4-栈方法-2"><a href="#_4-栈方法-2" class="header-anchor">#</a> 4.栈方法(2)</h5> <ol><li><p>push()</p> <p>可以接受任意数量的参数，并将他们添加到数组的末尾，返回值的数组的最新长度</p></li> <li><p>pop()</p> <p>不接收参数，弹出数组的最后一项，会改变原数组，返回值为弹出的元素</p></li></ol> <h5 id="_5-队列方法-2"><a href="#_5-队列方法-2" class="header-anchor">#</a> 5.队列方法(2)</h5> <ol><li><p>shift()</p> <p>不接受参数，删除数组的第一项并将其作为返回值，改变原数组</p></li> <li><p>unshift()</p> <p>可以接受任意数量的参数，并将他们添加到数组的开头，返回值的数组的最新长度</p></li></ol> <h5 id="_6-排序方法-2"><a href="#_6-排序方法-2" class="header-anchor">#</a> 6.排序方法(2)</h5> <ol><li><p>sort()</p> <ol><li><p>若不接受参数</p> <p>直接将元素按字符串形式进行比较，改变原数组</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> values<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">15</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">61</span><span class="token punctuation">]</span>
values<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>values<span class="token punctuation">)</span><span class="token comment">//[1,15,3,3,5,61]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li> <li><p>若接受回调函数为参数</p> <p>回调函数接受两个参数，分别为value1,value2,将数组所有的元素成对传入，如果返回值为负数，则value1应该在value2之前，如果返回正，则结果也相反，</p> <p>如果回调函数返回整数或0，则返回原顺序</p> <p>如果回调函数返回负数，则返回逆序数组，同reverse()</p></li></ol></li> <li><p>reverse()</p> <p>直接将原数组反转，改变原数组</p></li></ol> <h5 id="_7-操作方法-3"><a href="#_7-操作方法-3" class="header-anchor">#</a> 7.操作方法(3)</h5> <ol><li><p>concat()</p> <p>接受多个参数，</p> <ol><li><p>创建当前数组的副本，将接收到的参数依次添加到数组末尾，如果参数类型为数组，则将其拆分再添加，如果非数组，则直接添加</p></li> <li><p>返回副本数组</p> <p>可以通过识别数组参数的[Symbol.inConcatSpreadable]的值来决定是否拆分打平数组参数，</p> <p>若为true，则强制打平所有类数组参数</p> <p>若为false,则将该类数组直接添加到副本数组</p></li></ol></li> <li><p>slice(begin,end)</p> <p>返回原数组[begin,end)的元素，不改变原数组，如果只有一个参数，则返回begin开始的所有参数，若参数为负数，则将负参数加上length转为正数，</p></li> <li><p>splice()</p> <p>可以对数组进行删除，插入，替换操作</p> <p>接受多个参数，第一个参数为删除或者插入替换开始位置的索引值，第二个参数为从开始位置删除元素个数，其他参数都会作为插入、替换的元素,返回值为删除的元素数组，没有则为空数组</p></li></ol> <h5 id="_8-搜索和位置方法-5"><a href="#_8-搜索和位置方法-5" class="header-anchor">#</a> 8.搜索和位置方法(5)</h5> <ol><li><p>indexOf()</p></li> <li><p>lastIndexOf()</p></li> <li><p>includes()</p> <p>以上三个方法都接收两个参数，第一个参数为查找的元素，第二个参数为开始查找的位置，indexOf和includes都从数组的0号索引开始查找，lastIndexOf从数组的末尾开始查找，indexOf与lastIndexOf的返回值都为所查找元素在数组中的索引，没找到就为-1，includes()返回布尔值。(查找规则遵循全等===)</p></li> <li><p>find((item,index,arr)=&gt;{},context)</p></li> <li><p>findIndex((item,index,arr)=&gt;{},context)</p> <p>以上两种方法接受一个断言函数作为参数，断言函数接受三个参数，第一个参数为当前搜索的元素，第二个参数为当前搜索元素的索引值，第三个参数为当前搜索的数组,如果断言函数返回true，则停止搜索，find方法返回匹配成功的item,findIndex返回匹配成功的index</p> <p>上面两种方法也可以接受第一个参数来指定断言函数运行的上下文</p></li></ol> <h5 id="_9-迭代方法-5"><a href="#_9-迭代方法-5" class="header-anchor">#</a> 9.迭代方法(5)</h5> <ol><li><p>every((item,index,arr)=&gt;{},context)</p> <p>对数组的所有元素运行回调函数，如果所有回调的返回值都为true，则给方法返回true，否则为false</p></li> <li><p>some((item,index,arr)=&gt;{},context)</p> <p>对数组的所有元素运行回调函数，如果所有回调的返回值都为false，则给方法返回false，否则为true</p></li> <li><p>forEach((item,index,arr)=&gt;{},context)</p> <p>相当于使用for循环遍历数组，无返回值</p></li> <li><p>map((item,index,arr)=&gt;{},context)</p> <p>将每次运行回调函数产生的返回值作为新数组返回</p></li> <li><p>filter((item,index,arr)=&gt;{},context)</p> <p>将所有运行回调函数返回值为true的item项集合成新数组返回</p></li></ol> <h5 id="_10-归并方法-2"><a href="#_10-归并方法-2" class="header-anchor">#</a> 10.归并方法(2)</h5> <ol><li><p>reduce(callback,begin)</p></li> <li><p>reduceRight(callback,begin)</p> <p>callback(prev,item,index,arr)</p> <p>begin为遍历的开始位置，reduce从前往后，reduceRight从后往前，prev为上一次遍历的返回值，item为当前元素的值，index为当前遍历的位置，arr为所遍历的数组</p></li></ol></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">7/23/2022, 10:05:22 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/jiaojiao-blog/docs/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/" class="prev router-link-active">
          读书笔记
        </a></span> <span class="next"><a href="/jiaojiao-blog/docs/读书笔记/ES6迭代器.html">
          ES6迭代器(犀牛书)
        </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:0;" data-v-ac050c62></ul></main></div> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/jiaojiao-blog/assets/js/app.9b9a9edd.js" defer></script><script src="/jiaojiao-blog/assets/js/3.13686b80.js" defer></script><script src="/jiaojiao-blog/assets/js/1.7e378398.js" defer></script><script src="/jiaojiao-blog/assets/js/23.a86a53ea.js" defer></script>
  </body>
</html>
